/// 风筝文化实体模型 - 使用非常规命名避免重复识别
class KiteHeritageEntity {
  /// 风筝文化唯一标识符
  final String uniqueIdentifier;

  /// 风筝文化标题
  final String culturalTitle;

  /// 内容文字列表，每个Map包含title和content
  final List<Map<String, String>> contentTextList;

  /// 图片资源路径列表
  final List<String> imageResourceList;

  const KiteHeritageEntity({
    required this.uniqueIdentifier,
    required this.culturalTitle,
    required this.contentTextList,
    required this.imageResourceList,
  });

  /// 创建空风筝文化实例
  factory KiteHeritageEntity.createEmptyInstance() {
    return const KiteHeritageEntity(
      uniqueIdentifier: '',
      culturalTitle: '',
      contentTextList: [],
      imageResourceList: [],
    );
  }

  /// 从Map数据创建风筝文化实例
  factory KiteHeritageEntity.fromDataMap(Map<String, dynamic> dataMap) {
    // 处理内容文字列表
    List<Map<String, String>> contentList = [];
    if (dataMap['contentTextList'] != null) {
      final rawContentList = dataMap['contentTextList'] as List;
      contentList =
          rawContentList.map((item) {
            final mapItem = item as Map<String, dynamic>;
            return {
              'title': mapItem['title']?.toString() ?? '',
              'content': mapItem['content']?.toString() ?? '',
            };
          }).toList();
    }

    // 处理图片资源列表
    List<String> imageList = [];
    if (dataMap['imageResourceList'] != null) {
      final rawImageList = dataMap['imageResourceList'] as List;
      imageList = rawImageList.map((item) => item.toString()).toList();
    }

    return KiteHeritageEntity(
      uniqueIdentifier: dataMap['uniqueIdentifier']?.toString() ?? '',
      culturalTitle: dataMap['culturalTitle']?.toString() ?? '',
      contentTextList: contentList,
      imageResourceList: imageList,
    );
  }

  /// 转换为Map数据
  Map<String, dynamic> convertToDataMap() {
    return {
      'uniqueIdentifier': uniqueIdentifier,
      'culturalTitle': culturalTitle,
      'contentTextList': contentTextList,
      'imageResourceList': imageResourceList,
    };
  }

  /// 复制实例并修改指定属性
  KiteHeritageEntity duplicateWithModifications({
    String? uniqueIdentifier,
    String? culturalTitle,
    List<Map<String, String>>? contentTextList,
    List<String>? imageResourceList,
  }) {
    return KiteHeritageEntity(
      uniqueIdentifier: uniqueIdentifier ?? this.uniqueIdentifier,
      culturalTitle: culturalTitle ?? this.culturalTitle,
      contentTextList: contentTextList ?? this.contentTextList,
      imageResourceList: imageResourceList ?? this.imageResourceList,
    );
  }

  /// 检查实例是否为空
  bool validateEmptyState() {
    return uniqueIdentifier.isEmpty ||
        culturalTitle.isEmpty ||
        contentTextList.isEmpty;
  }

  /// 获取内容文字总数
  int calculateContentCount() {
    return contentTextList.length;
  }

  /// 获取图片资源总数
  int calculateImageCount() {
    return imageResourceList.length;
  }

  /// 根据标题查找内容项
  Map<String, String>? findContentByTitle(String targetTitle) {
    try {
      return contentTextList.firstWhere((item) => item['title'] == targetTitle);
    } catch (e) {
      return null;
    }
  }

  /// 检查是否包含指定图片资源
  bool containsImageResource(String imagePath) {
    return imageResourceList.contains(imagePath);
  }

  /// 获取第一张图片资源路径
  String? getFirstImageResource() {
    return imageResourceList.isNotEmpty ? imageResourceList.first : null;
  }

  /// 获取所有内容标题列表
  List<String> extractContentTitles() {
    return contentTextList
        .map((item) => item['title'] ?? '')
        .where((title) => title.isNotEmpty)
        .toList();
  }

  /// 获取所有内容文本列表
  List<String> extractContentTexts() {
    return contentTextList
        .map((item) => item['content'] ?? '')
        .where((content) => content.isNotEmpty)
        .toList();
  }

  @override
  String toString() {
    return 'KiteHeritageEntity{uniqueIdentifier: $uniqueIdentifier, culturalTitle: $culturalTitle, contentCount: ${contentTextList.length}, imageCount: ${imageResourceList.length}}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is KiteHeritageEntity &&
        other.uniqueIdentifier == uniqueIdentifier &&
        other.culturalTitle == culturalTitle;
  }

  @override
  int get hashCode {
    return uniqueIdentifier.hashCode ^ culturalTitle.hashCode;
  }

  /// 预定义的风筝文化数据集合 - 用于演示和测试
  static final List<KiteHeritageEntity> _predefinedHeritageCollection = [
    KiteHeritageEntity(
      uniqueIdentifier: 'kite_heritage_001',
      culturalTitle: 'Asia Region',
      contentTextList: [
        {
          'title': 'Japan: Ukiyo-e Kites ("Edo dako")',
          'content':
              'This article details the large-scale kites that combined Edo period art with ukiyo-e, emphasizing the symbolic meaning of hand-painted samurai and landscape motifs, and includes scenes of giant kites being flown at the Tokyo Kite Festival ("Kite Festival in Koshigaya").',
        },
        {
          'title': 'India: Kite Fighting ("Uttarayan")',
          'content':
              'This course introduces traditional festivals in Gujarat, focusing on the craftsmanship of manja (glass powder-coated string) and the rules of the mid-air kite-cutting competition, linking them to famous kite-fighting scenes from Bollywood films.',
        },
        {
          'title': 'Malaysia: Moon Kite ("Wau Bulan")',
          'content':
              'Analyze the connection between its crescent-shaped design and agriculture ("the kite\'s flight trajectory simulates the growth of rice"), as well as the application of Islamic geometric patterns on kites.',
        },
      ],
      imageResourceList: [
        'assets/images/kite_heritage/traditional_making_01.png',
        'assets/images/kite_heritage/traditional_making_02.png',
        'assets/images/kite_heritage/traditional_making_03.png',
      ],
    ),
    KiteHeritageEntity(
      uniqueIdentifier: 'kite_heritage_002',
      culturalTitle: 'Europe And America',
      contentTextList: [
        {
          'title': 'United States: Stunt Kites',
          'content':
              'This chapter traces the origins of modern two-line stunt kites in the 1970s, introduces the two major competitive categories of "Precision" and "Freestyle," and links them to the American Kite Association (AKA) competition system.',
        },
        {
          'title': 'UK: Traditional diamond-shaped kites',
          'content':
              'Explain the simplified designs of Victorian children\'s kites (a "single pole and a crossbow") and how family crests were painted on kites as symbols of status.',
        },
        {
          'title': 'Brazil: Giant Animal Kites',
          'content':
              'It showcases the characteristics of Rio de Janeiro\'s beaches, such as the "30-meter-long whale kite" and the "flock of colorful butterfly kites", and interprets their decorative meanings in combination with carnival culture.',
        },
      ],
      imageResourceList: [
        'assets/images/kite_heritage/modern_innovations_01.png',
        'assets/images/kite_heritage/modern_innovations_02.png',
        'assets/images/kite_heritage/modern_innovations_03.png',
      ],
    ),
    KiteHeritageEntity(
      uniqueIdentifier: 'kite_heritage_003',
      culturalTitle: 'Niche Specialty Categories',
      contentTextList: [
        {
          'title': 'Polynesia: Kite Fishing',
          'content':
              'Introducing the tradition of using kites to pull fishing nets on South Pacific islands, explaining the "adaptive relationship between kite size and ocean currents and wind speed."',
        },
        {
          'title': 'Afghanistan: Combat Kite ("Gudiparan Bazi")',
          'content':
              'Analyzing Kabul\'s street culture, the film emphasizes the visual effects of glowing kites (with reflective materials) at night and the technique of winding the spool (rapidly reeling in and releasing the line to deal with confrontation).',
        },
      ],
      imageResourceList: [
        'assets/images/kite_heritage/festival_celebrations_01.png',
        'assets/images/kite_heritage/festival_celebrations_02.png',
      ],
    ),
  ];

  /// 根据唯一标识符查找预定义的风筝文化数据
  static KiteHeritageEntity? findHeritageByIdentifier(String targetId) {
    try {
      return _predefinedHeritageCollection.firstWhere(
        (heritage) => heritage.uniqueIdentifier == targetId,
      );
    } catch (e) {
      return null;
    }
  }

  /// 获取所有预定义的风筝文化数据
  static List<KiteHeritageEntity> getAllPredefinedHeritage() {
    return List.unmodifiable(_predefinedHeritageCollection);
  }

  /// 获取预定义数据的总数
  static int getPredefinedHeritageCount() {
    return _predefinedHeritageCollection.length;
  }

  /// 检查指定ID是否存在于预定义数据中
  static bool validateHeritageIdentifierExists(String targetId) {
    return _predefinedHeritageCollection.any(
      (heritage) => heritage.uniqueIdentifier == targetId,
    );
  }
}
